package com.woniushop.dao.imple;

import java.util.List;

import com.woniushop.dao.AddressDAO;
import com.woniushop.dao.BaseDAO;
import com.woniushop.entity.Address;

/**
 * 地址DAO的实现类
 * 
 * @author 崔新雄
 *
 */
public class AddressDAOImple implements AddressDAO {

	private BaseDAO<Address> baseDAO = new BaseDAO<Address>();

	/**
	 * 将address对象加入数据库中
	 * 
	 * @param address
	 */
	@Override
	public void add(Address address) {
		String sql = "insert into address(uid,aphone,acode,ainfo,aname,astate) values(?,?,?,?,?,?)";
		Object[] objs = { address.getUid(), address.getAphone(), address.getAcode(), address.getAinfo(),
				address.getAname(), address.getAstate() };
		baseDAO.update(sql, objs);
	}

	/**
	 * 根据地址id删除数据库中对应的地址数据
	 * 
	 * @param aid
	 */
	@Override
	public void delete(Integer aid) {
		String sql = "delete from address where aid=?";
		Object[] objs = { aid };
		baseDAO.update(sql, objs);
	}

	/**
	 * 修改对应的地址数据
	 * 
	 * @param address
	 */
	@Override
	public void update(Address address) {
		String sql = "update address set uid=?,aphone=?,acode=?,ainfo=?,aname=?,astate=? where aid=?";
		Object[] objs = { address.getUid(), address.getAphone(), address.getAcode(), address.getAinfo(),
				address.getAname(), address.getAstate(), address.getAid() };
		baseDAO.update(sql, objs);
	}

	/**
	 * 根据对应的地址id查找地址数据
	 * 
	 * @param aid
	 * @return
	 */
	@Override
	public Address findOne(Integer aid) {
		String sql = "select a.*,u.uname,u.uaccount from address a, user u where a.uid=u.uid and aid=?";
		Object[] objs = { aid };
		List<Address> list = baseDAO.select(sql, Address.class, objs);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 查找用户的默认地址数据
	 * 
	 * @param uid
	 * @return
	 */
	@Override
	public Address findDefault(Integer uid) {
		String sql = "select a.*,u.uname,u.uaccount from address a, user u where a.uid=u.uid and a.uid=? and a.astate=1";
		Object[] objs = { uid };
		List<Address> list = baseDAO.select(sql, Address.class, objs);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 多条件分页查询地址数据
	 * 
	 * @param page
	 * @param pageSize
	 * @param address
	 * @return
	 */
	@Override
	public List<Address> findByPage(Integer page, Integer pageSize, Address address) {
		String sql = "select a.*,u.uname,u.uaccount from address a, user u where a.uid=u.uid";
		sql = finishSQL(address, sql);
		sql += " order by a.astate asc limit ?,? ";
		return baseDAO.select(sql, Address.class, (page - 1) * pageSize, pageSize);
	}

	/**
	 * 查找符合条件的地址数量
	 * 
	 * @param address
	 * @return
	 */
	@Override
	public Integer count(Address address) {
		String sql = "select count(*) from address a, user u where a.uid=u.uid";
		sql = finishSQL(address, sql);
		return baseDAO.count(sql);
	}

	/**
	 * 完成多条件查询的SQl语句
	 * 
	 * @param address
	 * @param sql
	 * @return
	 */
	private String finishSQL(Address address, String sql) {
		String acode = address.getAcode();
		if (acode != null && !acode.trim().equals("")) {
			sql += " and acode like '%" + acode + "%'";
		}
		String ainfo = address.getAinfo();
		if (ainfo != null && !ainfo.trim().equals("")) {
			sql += " and ainfo like '%" + ainfo + "%'";
		}
		String aname = address.getAname();
		if (aname != null && !aname.trim().equals("")) {
			sql += " and aname like '%" + aname + "%'";
		}
		String aphone = address.getAphone();
		if (aphone != null && !aphone.trim().equals("")) {
			sql += " and aphone like '%" + aphone + "%'";
		}
		Integer astate = address.getAstate();
		if (astate != null && astate < 4) {
			sql += " and  astate='" + astate + "'";
		} else if (astate != null && astate == 4) {// astate == 4表示查所有可用地址（包括默认）
			sql += " and  astate!='3'";
		}
		Integer uid = address.getUid();
		if (uid != null) {
			sql += " and  a.uid='" + uid + "'";
		}
		return sql;
	}

}
